#include "Plane.h"

namespace Geometry
{
	bool Plane::intersect(const Ray& _r, Point& _p, float& _t)
	{
		float n_dot_d = n().unit() * _r.q();
		float neg_n_dot_o_minus_q = -n() * ( _r.p() - p() );

		if( n_dot_d != 0 )
		{
			_t = neg_n_dot_o_minus_q / n_dot_d;

			if( _t > -0.01 )
			{
				Vector t_rq = _t * _r.q();
				_p = ( _t * _r.q() ) + _r.p();
				return true;
			}
			else
			{
				return false;
			}

		}
		else if( neg_n_dot_o_minus_q == 0  &&  n_dot_d == 0 )
		{
			_p = _r.p();
			_t = 0.0;
			return true;
		}
		else
		{
			return false;
		}
	}
}